package com.mobile.collect.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.mobile.collect.R;
import com.mobile.collect.beans.DevicePicInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.present.BasePresent;

import java.util.List;

import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;

public class CopyPicListActivity extends BaseListActivity<DevicePicInfo, BasePresent> {


    private Long deviceDbId;
    private Long targetPicDbId;
    private boolean isSingel;

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("选择模板");
    }

    @Override
    public BaseRecyclerAdapter<DevicePicInfo> getAdapter() {
        return new BaseRecyclerAdapter<DevicePicInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_picinfo;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                DevicePicInfo info = getItemByPosition(position);
                holder.setText(R.id.picNameTv, info.getUnitName()+"-"+info.getDeviceName());
                holder.setText(R.id.picNoTv, info.getLabel());
            }
        };
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        List<DevicePicInfo> list = EntityManager.getInstance().queryAllDevicePic(deviceDbId);
        onSuccess(list);
    }

    @Override
    public void itemClick(int position) {
        Long picDbId = mBaseAdapter.getItemByPosition(position).get_ID();
        PointListActivity.lunch(this,deviceDbId, picDbId, targetPicDbId, isSingel);
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public void getIntentData() {
        deviceDbId = getIntent().getLongExtra("parentid", -1);
        targetPicDbId = getIntent().getLongExtra("targetpicdbid", -1);
        isSingel = getIntent().getBooleanExtra("issingel", false);
    }

    @Override
    public BasePresent newP() {
        return new BasePresent();
    }

    public static void lunch(Activity context, Long deviceDbId, Long targetPicDbId, boolean isSingel) {
        Intent intent = new Intent(context, CopyPicListActivity.class);
        intent.putExtra("parentid", deviceDbId);
        intent.putExtra("targetpicdbid", targetPicDbId);
        intent.putExtra("issingel", isSingel);
        context.startActivityForResult(intent, 0x001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 0x001) {
            if (null != data) {
                setResult(Activity.RESULT_OK, data);
            } else {
                setResult(Activity.RESULT_OK);
            }
            finish();
        }
    }
    @Override
    public boolean isNotWorkConnect() {
        return false;
    }
}
